package com.mulanweb.api.module.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.jfinal.json.Json;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.db.common.WxParaFormatter;
import org.jsoup.Jsoup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/26.
 */
public class HotCommodityController extends BaseController {

    public void search() {
            Integer typeId= getParaToInt("typeId");
            SQLGenerateHelper helper = new SQLGenerateHelper("hot_commodity", "create_time desc");
            helper.addEqualsCondition("type_id",23);
            Page<HotCommodity> page = HotCommodity.dao.paginate(getPageNumber(), getPageSize(), helper.getSqlSelect(), helper.getSqlExceptSelect(), helper.getQueryParas());
            Page<Map<String,Object>> resultPages = new Page<>(new ArrayList<>(), page.getPageNumber(), page.getPageNumber(), page.getTotalPage(), page.getTotalRow());
            for (HotCommodity hh : page.getList()) {
                if (hh.getTypeId() == 23) {
                    Ticket ticket = Ticket.dao.findById(hh.getTargetId());
                    Map<String,Object> ticketMap = new HashMap<String,Object>();
                    ticketMap.put("content",ticket);
                    ticketMap.put("cityName",City.dao.findById(ticket.getCityId()).getNameCn());
                    resultPages.getList().add(ticketMap);
                } else if (hh.getTypeId() == 24) {
                    Chartered chartered= Chartered.dao.findById(hh.getTargetId());
                    Map<String,Object> charteredMap = new HashMap<String,Object>();
                    charteredMap.put("content",chartered);
                    charteredMap.put("cityName",City.dao.findById(chartered.getCityId()).getNameCn());
                    resultPages.getList().add(charteredMap);
                }
            }
            renderJson(resultPages);
    }
}
